package com.xh.core.util;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/4/12 09:20
 **/
public class HtmlUtils {
    public static String GT = "&gt;";
    //<
    public static String LT = "&lt;";
    //"
    public static String QUOT = "&quot;";
    //&
    public static String AMP = "&amp;";
    //空格
    public static String SPACE = "&nbsp;";
    //©
    public static String COPYRIGHT = "&copy;";
    //®
    public static String REG = "&reg;";
    //™
    public static String TM = "&trade;";
    //¥
    public static String RMB = "&yen;";

    public static String T1 = "&#39;";

    private static String regEx_startParagraph ="<[p|P].*?>([\\s]*)";

    private static String regEx_endParagraph ="([\\s]*)</[p|P]?>";

    /**
     * 删除script标签
     * @param str
     * @return
     */
    public static String delScriptTag(String str)
    {
        return str.replaceAll("(?i)<script[^>]*?>[\\s\\S]*?<\\/script>", "").trim();
    }

    /**
     * 删除style标签
     * @param str
     * @return
     */
    public static String delStyleTag(String str)
    {
        return str.replaceAll("(?i)<style[^>]*?>[\\s\\S]*?<\\/style>", "").trim();
    }

    /**
     * 删除HTML标签
     * @param str
     * @return
     */
    public static String delHTMLTag(String str)
    {
        String result=str.replaceAll("<[^>]+>", "").trim();
        result = cleanExceptImg(result);
        return result;
    }

    /**
     * 删除除img外所有HTML标签
     * @param str
     * @return
     */
    public static String delHTMLTagExceptImg(String str)
    {
        return str.replaceAll("(?i)<(?!img)[^>]*>", "").trim();
    }

    /**
     * 删除除img外所有标签
     * @param str
     * @return
     */
    public static String delAllTagExceptImg(String str)
    {
        //删script
        str = delScriptTag(str);
        //删style
        str = delStyleTag(str);
        //删HTML
        str = delHTMLTagExceptImg(str);
        return str;
    }

     /**
     * 清除除img外所有标签,恢复HTML转义字符
     * @param str
     * @return
     */
    public static String cleanExceptImg(String str)
    {
        str = delAllTagExceptImg(str);
        str = recoverHtmlChar(str);
        return str;
    }

    /**
     * 恢复HTML转义字符
     * @param str
     * @return
     */
    public static String recoverHtmlChar(String str) {
        str = str.replaceAll("(?i)"+SPACE, " ");
        str = str.replaceAll("(?i)"+GT, ">");
        str = str.replaceAll("(?i)"+LT, "<");
        str = str.replaceAll("(?i)"+QUOT, "\"");
        str = str.replaceAll("(?i)"+AMP, "&");
        str = str.replaceAll("(?i)"+COPYRIGHT, "©");
        str = str.replaceAll("(?i)"+REG, "®");
        str = str.replaceAll("(?i)"+TM, "™");
        str = str.replaceAll("(?i)"+RMB, "¥");
        str = str.replaceAll("(?i)"+T1, "'");
        return str;
    }

    /**
     * 替换标签为标签内属性值
     * @param str html
     * @param tag 标签
     * @param attr 属性
     * @return
     */
    public static String replaceTag2TagAttr(String str, String tag, String attr)
    {
        if (!StringUtils.isEmpty(str))
        {
            return str.replaceAll("(?i)<" + tag + "\\s+\\S*\\s+\\S*\\s+" + attr + "=\"([^\"]+)\"/>", "$1");
        }
        return "";
    }

}
